package com.rjkj.medicalwaste.viewmodels

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import com.rjkj.medicalwaste.base.BaseViewModel
import com.rjkj.medicalwaste.bean.*
import com.rjkj.medicalwaste.database.GarbageSetOutbound
import com.rjkj.medicalwaste.network.response.BaseResponse
import com.rjkj.medicalwaste.network.response.catchException
import com.rjkj.medicalwaste.network.response.deal
import com.rjkj.medicalwaste.network.response.next
import com.rjkj.medicalwaste.repository.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import retrofit2.http.Body

class OutboundViewModel(private val repository: GarbageRepository) : BaseViewModel() {
    val showHistoryInfo = MutableLiveData(false)
    val historyInfoList = MutableLiveData(listOf<HandoverInfoBean>())
    val waitHandoverGarbageList = MutableLiveData(listOf<WaitHandoverGarbageBean>())
    fun getPagingData(): Flow<PagingData<TestBean>> {
        return OutboundRepository.getOutboundPagingData()
    }

    fun getOutboundGarbage(roomId: String, hospitalId: String): LiveData<List<GarbageBean>> {
        return repository.getOutboundGarbage(roomId, hospitalId)
    }

    private suspend fun setOutboundGarbageStateToOutbound(list: List<GarbageSetOutbound>) {
        uploadEvent.postValue(true)
        repository.setOutboundGarbageStateToOutbound(list)
        uploadEvent.postValue(false)
    }

    suspend fun handover(
        url: String,
        codes: List<String>,
        handoverDeviceSN: String,
        handoverCleanerQRCode: String,
        handoverQRCode: String,
        handoverTime: String,
    ) {
        viewModelScope.launch {
            request(true) {
                handover(
                    url = url, HandoverReq(
                        garbageBoxQRCodes = codes,
                        handoverDeviceSN = handoverDeviceSN,
                        handoverCleanerQRCode = handoverCleanerQRCode,
                        handoverQRCode = handoverQRCode,
                        handoverTime = handoverTime
                    )
                )
            }.next {
                if (code == 200) {
                    tips.value = "出库成功"
                    repository.deleteGarbage(codes)
                } else {
                    tips.value = "出库失败"
                }
                getWaitHandoverGarbageList(
                    url = url,
                    handoverQRCode = handoverCleanerQRCode,
                    showTips = false,
                    serialNum = handoverDeviceSN
                )
                Log.e("data===>", "data=====>${this.data}")
            }.catchException {
                tips.postValue(this.message)
                Log.e("catchException===>", "catchException=====>${this}")
                printStackTrace()
            }

        }
    }

    suspend fun getWaitHandoverGarbageList2(
        url: String,
        handoverQRCode: String,
        serialNum: String,
    ): List<WaitHandoverGarbageBean>? {
        return request {
            getWaitHandoverGarbage(url = url, serialNum, handoverQRCode)
        }.deal()
    }


    suspend fun getHistoryInfo(url: String,serialNum:String) {
        request {
            handoverReport(url, serialNum)
        }.next {
            if (code == 200 && data != null) {
                historyInfoList.value = data
            } else {
                tips.value = "查询失败"
            }

        }.catchException {
            printStackTrace()
        }
    }

    suspend fun getWaitHandoverGarbageList(
        url: String,
        handoverQRCode: String,
        showTips: Boolean = true,
        serialNum: String,
    ) {
        request {
            getWaitHandoverGarbage(url = url, serialNum, handoverQRCode)
        }.next {
            if (code == 200 && data != null) {
                waitHandoverGarbageList.value = data
            } else {
                if (showTips)
                    tips.value = "获取待出库医废失败"
            }

        }.catchException {
            printStackTrace()
        }
    }
}